<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body>
    <h4 id="h4Dom"></h4>
    <input type="text" id="inputDom" oninput="changeValue(this.value)">
</body>
<script>
    /**
     * Object.defineProperty()
     */
    var user = {}
    
    // 数据劫持
    var _name = ''
    Object.defineProperty(user,'name',{
        get:function(){ // 取值
            console.log('执行get取值......')
            return _name+Math.random() // 扩展操作
        },
        set:function(value){ // 赋值
            console.log('执行set赋值......')
            _name = value.toUpperCase() // 扩展操作
        }
    })

    // user.name = 'alice'
    // console.log(user.name)


    /**
     * 响应式的简单实现
     */ 
    var _value = ''
    var data = {}
    Object.defineProperty(data,'value',{
        get(){
            return _value
        },
        set(newValue){
            _value = newValue
            let h4Dom = document.getElementById('h4Dom')
            h4Dom.textContent = _value
        }
    })
    
    function changeValue(newValue){
        data.value = newValue
    }

</script>
</html>